<?php

class Settings extends Controller {

	function Settings(){
		parent::Controller();	
	}
	function _setConstant(){
		$data['js1'] = 'jquery-1.4.2.js';
		$data['js2'] = 'jquery.form.js';
		$data['js3'] = "jquery.validate.pack.js";
		$data['aboutLink'] = base_url().'main/about';
		$data['contactLink'] = base_url().'main/contact';
		$data['helpLink'] = base_url().'main/help';
		return $data;
	}
	function index(){
		$this->info();
	}
	
	function info(){
		if(!$this->session->userdata('isLogin')){
				redirect('/home/');
		}else{
			$this->load->model('UsersDetail');
			if($this->input->post('submitInfo')){
				$uid = $this->session->userdata('userId');
				$tglLahir = date("Y-m-d",strtotime($this->input->post('tglLahir')));
				$about_me = $this->input->post('about_me');
				$gender = $this->input->post('gender');
				$location = $this->input->post('location');
				$this->UsersDetail->update($uid,$tglLahir,$about_me,$gender,$location);
			}
			$data = $this->_setConstant();
			$this->load->model('UserMahasiswa');
			$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
			$data['nim'] = json_decode($this->UserMahasiswa->getNim($this->session->userdata('userId')))->nim;
			$detail = json_decode($this->UsersDetail->getDetail($this->session->userdata('userId')));
			$data ['tglLahir'] = $detail->tglLahir;
			$data ['about_me'] = $detail->about_me;
			$data ['gender'] = $detail->gender;
			$data ['location'] = $detail->location;
			$data ['reg_date'] = $detail->reg_date;
			$data ['pageTitle'] = 'Pengaturan Info '.$data['nama'];
			$this->load->view('settingsInfo',$data);
		}
	}
	
	function avatar(){
		if(!$this->session->userdata('isLogin')){
				redirect('/home/');
		}else{
			$this->load->model('UsersDetail');
			$this->load->model('UserMahasiswa');
			$detail = json_decode($this->UsersDetail->getDetail($this->session->userdata('userId')));
			$data = $this->_setConstant();
			$data ['noError'] = TRUE;
			$data ['errorMsg'] = '';
			if($this->input->post('submitAvatar')){
				srand ((double) microtime( )*1000000);
				$fname = rand( );
				$config['upload_path'] = './sknimg/i-see-attr/avatar';
				$config['allowed_types'] = 'jpg|png';
				$config['max_size'] = '0';
				$config['file_name'] = md5($fname);
				$config['max_width'] = '0';
				$config['max_height'] = '0';
				$this->load->library('upload', $config);
				$data ['noError'] = $this->upload->do_upload('avatar');
				$data ['errorMsg'] = $this->upload->display_errors('<p>', '</p>');
				if($data ['noError']){
					$uid = $this->session->userdata('userId');
					$uploadData = $this->upload->data();
					$oldAvatar = $detail->avatar;
					$this->load->helper('file');
					if($uploadData['image_width']<200||$uploadData['image_height']<200){
						delete_file(realpath('./sknimg/i-see-attr/avatar'),md5($fname).$uploadData['file_ext']);
						$data ['noError'] = FALSE;
						$data ['errorMsg'] = "Ukuran Gambar Minimal 200px X 200px";
					}else{
						if($oldAvatar!=''){
							delete_file(realpath('./sknimg/i-see-attr/avatar'),$oldAvatar);
							delete_file(realpath('./sknimg/i-see-attr/avatarThumb'),$oldAvatar);
						}
						$config['image_library'] = 'gd2';
						$config['source_image'] = realpath("./sknimg/i-see-attr/avatar/".md5($fname).$uploadData['file_ext']."");
						$config['create_thumb'] = FALSE;
						$config['maintain_ratio'] = TRUE;
						$config['width'] = 50;
						$config['new_image'] = './sknimg/i-see-attr/avatarThumb/'.md5($fname).$uploadData['file_ext'];
						$this->load->library('image_lib', $config);
						$data ['noError'] = $this->image_lib->resize();
						$data ['errorMsg'] = $this->image_lib->display_errors();
						if($data ['noError']){
							$this->UsersDetail->updateAvatar($uid,md5($fname).$uploadData['file_ext']);
						}else{
							delete_file(realpath('./sknimg/i-see-attr/avatar'),md5($fname).$uploadData['file_ext']);
						}
						
						
					}
				}
			}elseif($this->input->post('deleteAvatar')){
				$uid = $this->session->userdata('userId');
				$oldAvatar = $detail->avatar;
				$this->load->helper('file');
				if($oldAvatar!=''){
					delete_file(realpath('./sknimg/i-see-attr/avatar'),$oldAvatar);
				}
				$this->UsersDetail->updateAvatar($uid,'');
			}
			$detail = json_decode($this->UsersDetail->getDetail($this->session->userdata('userId')));
			$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
			$data ['avatar'] = $detail->avatar;
			if($data['avatar']==''){
				$data['avatar'] = 'default_avatar.jpg';
			}
			$data ['pageTitle'] = 'Pengaturan Avatar '.$data['nama'];
			$this->load->view('settingsAvatar',$data);
		}
	}
	
	function contact(){
		if(!$this->session->userdata('isLogin')){
				redirect('/home/');
		}else{
			$this->load->model('UsersDetail');
			$data = $this->_setConstant();
			$data ['noError'] = TRUE;
			$data ['errorMsg'] = '';
			if($this->input->post('submitContact')){
				$uid = $this->session->userdata('userId');
				$this->UsersDetail->updateContact($uid,
									$this->input->post('website'),
									$this->input->post('work_phone'),
									$this->input->post('personal_phone'),
									$this->input->post('im_skype'),
									$this->input->post('im_gtalk'),
									$this->input->post('im_msn'),
									$this->input->post('im_yahoo'),
									$this->input->post('im_aim'),
									$this->input->post('prof_facebook'),
									$this->input->post('prof_twitter'),
									$this->input->post('prof_flickr'),
									$this->input->post('prof_friendfeed'),
									$this->input->post('prof_myspace'),
									$this->input->post('prof_youtube'));
			}
			$this->load->model('UserMahasiswa');
			$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
			$detail = json_decode($this->UsersDetail->getDetail($this->session->userdata('userId')));
			$data['website'] = $detail->website;
			$data['work_phone'] = $detail->work_phone;
			$data['personal_phone'] = $detail->personal_phone;
			$data['im_skype'] = $detail->im_skype;
			$data['im_gtalk'] = $detail->im_gtalk;
			$data['im_msn'] = $detail->im_msn;
			$data ['im_msn']= $detail->im_msn;
  			$data ['im_yahoo']= $detail->im_yahoo;
  			$data ['im_aim']= $detail->im_aim;
  			$data ['prof_facebook']= $detail->prof_facebook;
  			$data ['prof_twitter']= $detail->prof_twitter;
  			$data ['prof_flickr']= $detail->prof_flickr;
  			$data ['prof_friendfeed']= $detail->prof_friendfeed;
  			$data ['prof_myspace']= $detail->prof_myspace;
  			$data ['prof_youtube']= $detail->prof_youtube;
			$data['email'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->email;
			$data ['pageTitle'] = 'Pengaturan Contact '.$data['nama'];
			$this->load->view('settingsContact',$data);
		}
	}
	
	function password(){
		if(!$this->session->userdata('isLogin')){
				redirect('/home/');
		}else{
			$this->load->model('UsersDetail');
			$this->load->model('User');
			$data = $this->_setConstant();
			$data ['noError'] = TRUE;
			$data ['errorMsg'] = '';
			if($this->input->post('submitPassword')){
				$uid = $this->session->userdata('userId');
				if(md5($this->input->post('old_pwd'))!=$this->User->getPassword($uid)){
					$data ['noError'] = FALSE;
					$data ['errorMsg'] = 'Old Password Salah';
				}elseif($this->input->post('new_pwd')!=$this->input->post('conf_new_pwd')){
					$data ['noError'] = FALSE;
					$data ['errorMsg'] = 'New Password Tidak Sama';
				}elseif($this->input->post('new_pwd')==''||strlen($this->input->post('new_pwd'))<6){
					$data ['noError'] = FALSE;
					$data ['errorMsg'] = 'New Password Tidak boleh kosong dan panjang password minimal 6 karakter';
				}else{
					$this->User->updatePassword($uid,md5($this->input->post('new_pwd')));
					$data ['noError'] = FALSE;
					$data ['errorMsg'] = 'Password berhasil di update';
				}
				
			}
			$this->load->model('UserMahasiswa');
			$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
			$detail = json_decode($this->UsersDetail->getDetail($this->session->userdata('userId')));
			$data ['pageTitle'] = 'Pengaturan Password '.$data['nama'];
			$this->load->view('settingsPassword',$data);
		}
	}
	
		
}
